package com.flower.controller;

import com.flower.service.UploadService;
import com.common.baseModel.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author 吃麻椒的花酱
 * @Date 2022/3/16 11:21
 * @Version 1.0
 */
@RestController
@Slf4j
@Api("图片上传")
@RequestMapping("/otherService/system/upload")
public class UploadController {

    Lock lock=new ReentrantLock();
    @Autowired
    UploadService uploadService;

    @Resource(name = "executer")
    @Autowired
    ThreadPoolExecutor executer;
    @ApiOperation(value = "图片存储")
    @PostMapping("ossUploadFile")
    public JsonResult<String> uploadFile(@ApiParam(name = "file",value = "图片上传" ,required = true) MultipartFile file){
        String imageUrl =uploadService.uploadFile(file);
        return JsonResult.create(imageUrl);
    }

    /*
     * @author 吃麻椒的花酱
     * @date  2021/7/10 1:54
     * @Description 接收什么样的参数，就应该使用什么样的注解，必须严格定义 以防止swagger测试文档报错，或者前端传值时出现错误
     * @param url
     * @return com.fmjava.utils.ResponseResult
     */
    @ApiOperation(value = "删除图片")
    @GetMapping("/deleteImage")
    public JsonResult<Object> deleteFile(@ApiParam(name = "url", value = "删除图片路径",required = true) @RequestParam String url){
        uploadService.deleteFile(url);
        return JsonResult.isOk();
    }
}
